package com.rrd.dw.mr.gzip.kafka;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.rrd.dw.utils.DateUtil;
import com.ucredit.babel.common.model.BaseModel;
import com.ucredit.babel.common.model.RecordType;
import com.ucredit.babel.common.model.nifa.NifaData;
import com.ucredit.babel.common.model.nifa.NifaQueryRecord;
import org.apache.avro.data.Json;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper.Context;

import java.io.IOException;
import java.util.Date;

/**
 * 中互金查询详情
 *
 * @author xj
 */

public class NifaQueryDetailParse extends ParseBase {
    private Text val = new Text();
    private Text key = new Text();

    /**
     * 中互金查询详情
     *
     * @param model
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    public boolean parseJob(Context context, BaseModel<?> model, String successful) throws IOException, InterruptedException {
        NifaQueryRecord obj = (NifaQueryRecord) model;
        NifaData data;
        JSONObject info;
        try {
            data = obj.getData();
            info = JSON.parseObject(data.getData());
        } catch (java.lang.NullPointerException ignored) {
            return true;
        }
        StringBuilder sb = getBaseInfo(obj, successful);
        StringBuilder txSb = getBaseInfo(obj);
        StringBuilder detailSb = new StringBuilder(200);

        // 明细信息
        JSONArray queryBeans = info.getJSONArray("infoquerybean") == null? new JSONArray(): info.getJSONArray("infoquerybean");
        for (Object qb: queryBeans) {
            detailSb.delete(0, detailSb.length());
            JSONObject queryBean = (JSONObject) qb;
            detailSb.append(queryBean.getString("ordernum") == null ? "" : queryBean.getString("ordernum")).append(CRT001);
            detailSb.append(queryBean.getString("ddate") == null ? "" : queryBean.getString("ddate")).append(CRT001);
            detailSb.append(queryBean.getString("s_value") == null ? "" : queryBean.getString("s_value")).append(CRT001);
            key.set(getKey());
            val.set((sb.toString() + detailSb.toString() + txSb.toString()).replace("\n", " ").replace("\r", " "));

            context.write(key, val);
        }

        return true;
    }

    @Override
    public String getSubType() {
        return ".detail";
    }

    @Override
    public String getTableName() {
        return "kafka_babel_nifa_query_detail_inc";
    }

    @Override
    public String getType() {
        return RecordType.NIFA_QUERY.toString();
    }

    @Override
    public String getComment() {
        return "中互金查询详情";
    }
}
